import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_module/common/base/base_page.dart';
import 'package:flutter_module/common/widget/dot_divider.dart';
import 'package:flutter_module/model/transaction_list_mo.dart';
import 'package:flutter_module/model/transaction_mo.dart';

/// Create by kyluzoi
/// Date: 2021-08-11
///
/// Description:
///

class TransactionRecordItem extends BaseStatelessWidget {
  final TransactionMo item;
  final int index;

  const TransactionRecordItem(
      {Key? key, required this.item, required this.index})
      : super(key: key);

  @override
  Widget createView(BuildContext context) {
    return Container(
      // height: 120.0,
      margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
      padding: EdgeInsets.fromLTRB(10, 10, 10, 15),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [],
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.grey.shade300, width: 1),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  _getIcomImage(),
                  SizedBox(width: 8),
                  Text(item.title ?? ""),
                ],
              ),
              Text('¥' + (item.amount ?? 0).toStringAsFixed(2)),
            ],
          ),
          SizedBox(height: 8),
          DotDivider(),
          SizedBox(height: 10),
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: _getRowRecords(),
          ),
          _getBtnRow(),
        ],
      ),
    );
  }

  //根据状态获取图标
  Widget _getIcomImage() {
    var imageName = '';
    switch (item.type) {
      case OrderType.course:
        {
          if (item.sourceType == 0) {
            imageName = 'course_transaction_icon.png';
          } else {
            imageName = 'activity_goods_icon.png';
          }
          break;
        }
      case OrderType.brands:
        {
          imageName = 'mine_brandmanager_icon.png';
          break;
        }
      case OrderType.factoryType:
        {
          imageName = 'identity_factory_icon.png';
          break;
        }
      case OrderType.celebrity:
      case OrderType.celebrityOrg:
        {
          imageName = 'mine_redman_record_icon.png';
          break;
        }
      case OrderType.serviceProvider:
        {
          imageName = 'serviceProvider_red_icon.png';
          break;
        }
      default:
        {
          imageName = 'identity_member_icon.png';
        }
    }
    // return Icon(Icons.access_alarm_outlined);
    return Image.asset(
      "images/transaction/$imageName",
      width: 18,
      height: 18,
      fit: BoxFit.cover,
    );
  }

  Widget _getBtnRow() {
    Widget? btn;
    if (item.allowInvoice == 1) {
      btn = TransactionBtn(
        title: '申请开票',
        onTap: () {
          print("申请开票");
        },
      );
    } else if (item.allowInvoice == 2) {
      btn = TransactionBtn(
        title: '查看发票',
        onTap: () {
          print("查看发票");
        },
      );
    }

    switch (item.type) {
      case OrderType.course:
      case OrderType.brands:
      case OrderType.celebrity:
      case OrderType.celebrityOrg:
      case OrderType.factoryType:
      case OrderType.serviceProvider:
        {
          break;
        }
      default:
        {
          if (item.status == '3') {
            btn = TransactionBtn(
              title: '修改凭证',
              onTap: () {
                print("修改凭证");
              },
            );
          } else if (item.status == '4') {
            btn = TransactionBtn(
              title: '立即支付',
              onTap: () {
                print("立即支付");
              },
            );
          }
        }
    }

    if (btn != null) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [btn],
      );
    } else {
      return SizedBox.shrink();
    }
    // if( item.allowInvoice == 1){

    // }
  }

  List<Widget> _getRowRecords() {
    List<Widget> widgetList = [];
    switch (item.type) {
      case OrderType.course:
        {
          widgetList.add(
            RecordText(
              text: (item.sourceType == 0 ? '交易' : '支付') + '时间:',
              subText: item.createTime.toString(),
            ),
          );
          if (item.payType != null) {
            widgetList.add(RecordText(text: '支付方式:', subText: item.payType!));
          }
          if (item.orderCode != null) {
            widgetList.add(RecordText(text: '订单编号:', subText: item.orderCode!));
          }
          widgetList.add(
            RecordText(
              text: (item.sourceType == 0 ? '课程名称' : '购买内容') + ':',
              subText: item.orderName ?? '',
            ),
          );
          break;
        }
      case OrderType.brands:
        {
          widgetList.add(
              RecordText(text: '缴费时间:', subText: item.createTime.toString()));
          if (item.payType != null) {
            widgetList.add(RecordText(text: '支付方式:', subText: item.payType!));
          }
          if (item.orderCode != null) {
            widgetList.add(RecordText(text: '订单编号:', subText: item.orderCode!));
          }
          widgetList.add(
              RecordText(text: '品牌生效时间:', subText: item.startTime.toString()));
          widgetList.add(
              RecordText(text: '品牌到期时间:', subText: item.endTime.toString()));
          break;
        }
      case OrderType.celebrity:
      case OrderType.celebrityOrg:
      case OrderType.factoryType:
        {
          widgetList.add(
              RecordText(text: '缴费时间:', subText: item.createTime.toString()));
          if (item.payType != null) {
            widgetList.add(RecordText(text: '支付方式:', subText: item.payType!));
          }
          if (item.orderCode != null) {
            widgetList.add(RecordText(text: '订单编号:', subText: item.orderCode!));
          }
          widgetList.add(
              RecordText(text: '会员生效时间:', subText: item.startTime.toString()));
          widgetList.add(
              RecordText(text: '会员到期时间:', subText: item.endTime.toString()));
          break;
        }
      case OrderType.serviceProvider:
        {
          widgetList.add(
              RecordText(text: '缴费时间:', subText: item.createTime.toString()));
          if (item.payType != null) {
            widgetList.add(RecordText(text: '支付方式:', subText: item.payType!));
          }
          if (item.orderCode != null) {
            widgetList.add(RecordText(text: '订单编号:', subText: item.orderCode!));
          }
          widgetList.add(
              RecordText(text: '生效时间:', subText: item.startTime.toString()));
          widgetList
              .add(RecordText(text: '到期时间:', subText: item.endTime.toString()));
          break;
        }
      default:
        {
          String timeTitle = "提交时间";
          bool isOfflinePayment = (item.payType ?? "").startsWith('线下');

          if (item.status != '0' && item.status != '4') {
            if (isOfflinePayment) {
              timeTitle = '审核时间';
            } else {
              timeTitle = '缴费时间';
            }
          }
          widgetList.add(RecordText(
              text: timeTitle + ':', subText: item.createTime.toString()));
          if (item.payType != null) {
            widgetList.add(RecordText(text: '支付方式:', subText: item.payType!));
          }
          if (item.orderCode != null) {
            widgetList.add(RecordText(text: '订单编号:', subText: item.orderCode!));
          }
          switch (item.status) {
            case "0":
              {
                widgetList.add(RecordText(
                  text: '订单状态:',
                  subText: item.payType ?? "" + "待审核",
                  subTitleColor: Color(0xFFA200),
                ));
                break;
              }
            case "1":
              {
                if (isOfflinePayment) {
                  widgetList.add(RecordText(
                    text: '订单状态:',
                    subText: item.payType ?? "" + "已通过",
                    subTitleColor: Color(0x14e371),
                  ));
                }

                widgetList.add(RecordText(
                    text: '会员生效时间:', subText: item.startTime.toString()));
                widgetList.add(RecordText(
                    text: '会员到期时间:', subText: item.endTime.toString()));
                break;
              }
            case "3":
              {
                widgetList.add(RecordText(
                  text: '订单状态:',
                  subText: item.payType ?? "" + "已拒绝",
                  subTitleColor: Color(0xef5d5e),
                ));

                var remark = item.remark as String;
                if (remark.isNotEmpty && remark.length > 0) {
                  widgetList
                      .add(RecordText(text: '拒绝原因:', subText: item.remark!));
                }
                break;
              }
            case "4":
              {
                if (item.payType != null) {
                  widgetList
                      .add(RecordText(text: '订单编号:', subText: item.orderCode!));
                  widgetList.add(RecordText(
                    text: '订单状态:',
                    subText: item.payType ?? "" + "待支付",
                    subTitleColor: Color(0xef5d5e),
                  ));
                } else {
                  widgetList.add(RecordText(
                    text: '订单状态:',
                    subText: item.payType ?? "" + "待支付",
                    subTitleColor: Color(0xef5d5e),
                  ));
                }
                break;
              }
            default:
              break;
          }
        }
    }

    return widgetList;
  }
}

class RecordText extends StatelessWidget {
  final String text;
  final String subText;
  final Color subTitleColor;

  RecordText({
    required this.text,
    this.subText = '',
    this.subTitleColor = Colors.blueGrey,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 1, 0, 0),
      child: Row(
        children: [
          Text(
            text,
            style: TextStyle(
              color: Colors.blueGrey,
              fontSize: 12,
            ),
          ),
          SizedBox(width: 3),
          Text(
            subText,
            style: TextStyle(
              color: subTitleColor,
              fontSize: 12,
            ),
          )
        ],
      ),
    );
  }
}

class TransactionBtn extends StatelessWidget {
  TransactionBtn(
      {required this.title,
      this.backgroundColor = Colors.red,
      required this.onTap});

  final String title;
  final Color backgroundColor;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 90,
      height: 30,
      child: MaterialButton(
        elevation: 0,
        color: backgroundColor,
        textColor: Colors.white,
        child: Text(title),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(5))),
        onPressed: onTap,
      ),
    );
  }
}
